<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../react18/react.development.js"></script>
    <script src="../react18/react-dom.development.js"></script>
    <script src="../react18/babel.min.js"></script>
    <script src="../react18/prop-types.js"></script>
</head>

<body>
    <div id="app"></div>
    <script type="text/babel">
        class App extends React.Component {
            state = {
                name: "laowang",
                age: 18,
                hobby: "吃饭"
            }
            render() {
                const { name, age, hobby } = this.state
                return (
                    <div>
                        姓名: <input type="text" value={name} onChange={this.changeName} />
                        <br />
                        年龄: <input type="text" value={age} onChange={this.changeAge} />
                        <br />
                        爱好: <input type="text" value={hobby} onChange={this.changeHobby} />
                        <br />
                        <button onClick={this.handle}>提交</button>
                    </div>
                )
            }


            //每一个表单需要单独获取value,否则value重复,无法获取完整的值
            //e事件对象中保存的是与当前事件相关的很多信息
            changeName = (e) => {
                //获取当前表单的值
                const { value } = e.target
                //将新值设置给state
                this.setState({ name: value })
            }

            //如果类型限制中的age是数值类型,可以在获取之后按如下方式转换为数值
            changeAge = (e) => {
                const { value } = e.target;
                this.setState({ age: +value })
            }
            changeHobby = (e) => {
                const { value } = e.target;
                this.setState({ hobby: value })
            }

            handle = () => {
                const { name, age, hobby } = this.state;
                console.log(name, age, hobby);
            }

        }

        const root = ReactDOM.createRoot(document.getElementById('app'));
        root.render(<App />)
    </script>

</body>

</html>